package cn.darkhorse.mesadmin.controller.device;


import cn.darkhorse.device.dto.MesDeviceTypeDTO;
import cn.darkhorse.device.pojo.MesDevice;
import cn.darkhorse.device.pojo.MesDeviceType;
import cn.darkhorse.device.service.MesDeviceService;
import cn.darkhorse.device.service.MesDeviceTypeService;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlLike;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

import static cn.darkhorse.common.constant.CommonConstant.ORDER_TYPE_DESC;

/**
 * <p>
 * 设备类型表 前端控制器
 * </p>
 *
 * @author yuanjun
 * @since 2022-11-10
 */
@Api(tags = "设备类型管理")
@RestController
@RequestMapping("/device/mesDeviceType")
public class MesDeviceTypeController extends BaseController {
    @Resource
    private MesDeviceTypeService mesDeviceTypeService;

    @Resource
    private MesDeviceService mesDeviceService;

    /**
     * 获取设备类型编码
     * @return
     */
    @ApiOperation(value = "获取设备类型编码")
    @RequestMapping("/getDeviceTypeCode")
    public AjaxResult getDeviceTypeCode() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",System.currentTimeMillis());
        return AjaxResult.success(jsonObject);
    }

    /**
     * 添加或修改设备类型
     * @param mesDeviceType
     * @return
     */
    @ApiOperation(value = "添加或修改设备类型")
    @PostMapping("/save")
    public AjaxResult save(@Validated @RequestBody MesDeviceTypeDTO mesDeviceType) {
        // Step.1 校验同公司下类型名称不可重复
        LambdaQueryWrapper<MesDeviceType> qw4DeviceType = new LambdaQueryWrapper<>();
        qw4DeviceType.eq(MesDeviceType::getTypeName, mesDeviceType.getTypeName().trim());
        if (mesDeviceType.getId() != null) {
            qw4DeviceType.ne(MesDeviceType::getId, mesDeviceType.getId());
        }
        List<MesDeviceType> types = mesDeviceTypeService.list(qw4DeviceType);
        if (null != types && !types.isEmpty()) {
            throw new ServiceException("农机类型名称已存在");
        }

        // Step.2 保存
        if (null == mesDeviceType.getCreator()) {
            mesDeviceType.setCreator(SecurityUtils.getUserId());
        }

        // 修改需要更新修改时间
        if(null != mesDeviceType.getId()) {
          mesDeviceType.setGmtModify(LocalDateTime.now());
        }

        MesDeviceType entity = new MesDeviceType();
        BeanUtils.copyProperties(mesDeviceType, entity);

        boolean result = mesDeviceTypeService.saveOrUpdate(entity);
        return AjaxResult.success(result);
    }

    /**
     * 根据设备类型Id获取设备类型信息
     * @param id 设备类型id
     * @return
     */
    @ApiOperation(value = "根据设备类型Id获取设备类型信息")
    @RequestMapping("/getMesDeviceType")
    public AjaxResult getMesDeviceType(Long id) {
        MesDeviceType deviceType = mesDeviceTypeService.getById(id);
        return AjaxResult.success(deviceType);
    }

    /**
     * 删除设备类型
     * @param id
     * @return
     */
    @ApiOperation(value = "删除设备类型")
    @RequestMapping("/del")
    public AjaxResult delMesDeviceType(Long id) {
        // Step.1 验证该类型下是否有设备，如有设备不允许删除
        LambdaQueryWrapper<MesDevice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MesDevice::getType, id);
        List<MesDevice> devices = mesDeviceService.list(queryWrapper);
        if (devices!= null && devices.size() > 0) {
            throw new ServiceException("类型下存在设备时不允许删除");
        }

        // Step.2 删除
        boolean result = mesDeviceTypeService.removeById(id);
        return AjaxResult.success(result);
    }

    /**
     * 根据类型名称模糊查询设备类型列表信息
     * @param typeName 类型名称
     * @param orderType 排序 asc desc
     * @return
     */
    @ApiOperation(value = "根据类型名称模糊查询设备类型列表信息")
    @RequestMapping("/list")
    public TableDataInfo listMesDeviceType(String typeName, String orderType) {
        startPage();
        LambdaQueryWrapper<MesDeviceType> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(typeName)) {
            queryWrapper.like(MesDeviceType::getTypeName, typeName);
        }

        if (ORDER_TYPE_DESC.equals(orderType)) {
            queryWrapper.orderByDesc(MesDeviceType::getGmtCreate);
        }

        queryWrapper.orderByAsc(MesDeviceType::getGmtCreate);
        List<MesDeviceType> types = mesDeviceTypeService.list(queryWrapper);
        return getDataTable(types);
    }

    public static void main(String[] args) {
        MesDeviceType type = new MesDeviceType();
        type.setCode("类型编码");
        type.setCreator(1L);
        type.setTypeName("类型名称");
        System.out.println(JSON.toJSONString(type));

    }
}

